package com.yiyi.credit.manage.sso.manager.controller;

import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.model.dto.response.Response;
import com.yiyi.credit.manage.sso.model.entity.SysResource;
import com.yiyi.credit.manage.sso.service.inner.SysResourceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class SysResourceController {

    //private static Logger logger = LoggerFactory.getLogger(SysResourceController.class);

    @Autowired
    SysResourceService sysResourceService;


    @RequestMapping(value = "/getSysResourceList")
    public Object getSysResourceList(@RequestParam Long sysInfoId) throws Exception {

        List<SysResource> sysResourceList = sysResourceService.getSysResourceBySysId(sysInfoId);
        for (SysResource sysResource : sysResourceList) {
            if (StringUtils.isNotBlank(sysResource.getUrl())) {
                sysResource.setUrl(null);
            }
        }
        return new Response<>(sysResourceList);
    }


    @RequestMapping(value = "/getSysResource")
    public Object getSysResource(@RequestParam Long id) throws Exception {

        SysResource sysResource = sysResourceService.getSysResourceById(id);
        return new Response<>(sysResource);
    }


    @RequestMapping(value = "/deleteSysResource")
    public Object deleteSysResource(@RequestParam Long id) throws Exception {

        sysResourceService.deleteSysResourceById(id);
        return new Response<>();
    }


    @RequestMapping(value = "/addSysResource")
    public Object addSysResource(@RequestBody SysResource sysResource) throws Exception {

        sysResourceService.addSysResource(sysResource);
        return new Response<>();
    }


    @RequestMapping(value = "/updateSysResource")
    public Object updateSysResource(@RequestBody SysResource sysResource) throws Exception {
        if (null == sysResource.getId()) {
            return new Response<>(ResponseCode.PARAM_ERR);
        }
        sysResourceService.updateSysResource(sysResource);
        return new Response<>();
    }

}
